import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '@/components/index'
import home from '@/components/home'
import holler from '@/components/holler';
import users from '@/components/users/users';
import rights from '@/components/rights/rights';
import roles from '@/components/rights/roles';
import cate from '@/components/goods/cate';

import Params from '@/components/goods/Params.vue'
import GoodsList from '@/components/goods/List.vue'
import Add from '@/components/goods/Add.vue'
import reports   from '@/components/reports/reports'
import orders from '@/components/orders/orders'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    //重定向
    redirect:"/index",
  },
  {
    path: '/index',
    component: index
  },
  {
    path: '/home',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: home,
    redirect: '/holler',
    children:[
      { path: '/holler', component: holler },
      { path: '/users', component: users },
      {path: '/rights',component: rights},
      {path: '/roles',component: roles},
      {path: '/categories',component: cate},
      { path: '/params', component: Params },
      { path: '/goods', component: GoodsList },
      { path: '/goods/add', component: Add },
      { path: '/reports', component: reports},
      { path: '/orders', component: orders}
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//路由导航守卫
router.beforeEach((to,from,next) =>{
    //to表示到达的路径
    //from从那个路劲跳转过来的
    //next是一个函数
    //登录页面任何人都可以访问
    if(to.path == '/index'){//这句话表示要访问index页面
      next()   //可以访问
    }else{
      //如果访问的不是登录页面就需要验证
      //判断当前要访问的页面是否有token，如果有就可以访问 没有就跳转到登录页面
      if(sessionStorage.token){
        //判断token是否存在   表示存在
        next()
      }else{
        //没有token跳转登录页面
        next('/index')//如果传递参数就跳到对应路由
      }
    }
}) 
export default router
